#include "drv_time.h"

/*********************************

函数名称:drv_time_init

函数功能:基础定时器TIM6初始化

函数参数:无

其它说明:

*********************************/

void drv_time6_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定义基本定时器结构体
	NVIC_InitTypeDef NVIC_InitStruct ;
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM6, ENABLE);//开启时钟

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;//时钟分割
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;//计数模式
	TIM_TimeBaseInitStruct.TIM_Period = 10000 ; //装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1 ;//预分频
	
	TIM_TimeBaseInit( TIM6 , &TIM_TimeBaseInitStruct);//初始化

	TIM_ITConfig( TIM6, TIM_IT_Update, ENABLE) ;//定时器更新使能

	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn ;//设置通道号
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1 ;//设置抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1 ;//设置响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE ;//使能

	NVIC_Init(&NVIC_InitStruct);//初始化中断

	TIM_Cmd( TIM6, ENABLE) ;//开启TIM6，使能
}

void drv_time1_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;//结构体定义
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//时钟使能
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//配置信息
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1);//设置GPIOE具体功能复用

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数模式
	TIM_TimeBaseInitStruct.TIM_Period = 500 ;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 1-1;//预分频
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);//初始化参数


	TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;//空闲时为高
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//设置模式为PWM1
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;//输出极性（有效电平）
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//使能输出
	TIM_OCInitStruct.TIM_Pulse = 0;//比较初始值
	TIM_OC1Init(TIM1, &TIM_OCInitStruct);

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//使能CCR影子寄存器
	TIM_ARRPreloadConfig(TIM1, ENABLE);//使能装载影子寄存器
	TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能PWM输出
	TIM_Cmd(TIM1, ENABLE);//定时器，使能
}
